package com.ding;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ding.commander.Commander;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Scanner;
import java.util.logging.Logger;

public class CommanderMain {

    private static Logger logger = Logger.getLogger("");

    //读取配置文件
    public static void main(String[] args) throws Exception {


   /*     if (true) {

            readConfig(new File("C:\\Users\\dinglei\\Desktop\\mynat-commander.conf.json"));

            return;
        }
*/
        String path = CommanderMain.class.getProtectionDomain().getCodeSource().getLocation().getFile();
        File file = new File(path);
        boolean flag = false;
        if (file.exists()) {
            File root = file.getParentFile();
            File configFile = new File(root, "mynat.conf");
            if (configFile.exists()) {
                try {
                    readConfig(configFile);
                } catch (Exception e) {
                    System.out.println("配置文件解析失败,请指定其他配置文件:");
                    Scanner scanner = new Scanner(System.in);
                    String line;
                    while ((line = scanner.nextLine()) != null) {
                        if (line.equals("EOF")) {
                            break;
                        }
                        configFile = new File(line);
                        if (!configFile.exists()) {
                            System.out.println("指定的路径不存在,请重新输入:");
                            continue;
                        }
                        try {
                            readConfig(configFile);
                            break;
                        } catch (Exception e1) {
                            System.out.println("配置文件解析失败,请指定其他配置文件:");
                        }
                    }
                    scanner.close();
                }
                return;
            }
        }

        System.out.println("未找到配置文件,请手动指定配置文件路径:");
        Scanner scanner = new Scanner(System.in);
        String line;
        while ((line = scanner.nextLine()) != null) {
            File f = new File(line);
            if (f.exists()) {
                try {
                    readConfig(f);
                    break;
                } catch (Exception e) {
                    System.out.println("配置文件解析失败请重新输入:");
                }
            } else {
                System.out.println("指定的路径不存在,请重新输入:");
            }
        }
        scanner.close();

    }


    private static void readConfig(File file) throws Exception {
        BufferedReader reader = new BufferedReader(new FileReader(file));
        String line;
        StringBuilder s = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            s.append(line);
        }
        reader.close();
        JSONArray objects = JSONArray.parseArray(s.toString());
        for (Object object : objects) {
            JSONObject jsonObject = (JSONObject) object;
            try {
                Commander center = jsonObject.toJavaObject(Commander.class);
                center.start();
            } catch (Exception e) {
                System.err.println("创建失败:" + jsonObject);
                System.err.println(e);
            }
        }

    }


}

